import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:honeypotflutter/modules/deliver/api/deliver_api.dart';
import 'package:honeypotflutter/modules/deliver/help/deliver_help.dart';
import 'package:honeypotflutter/modules/deliver/pages/deliver_express/view.dart';
import 'package:honeypotflutter/modules/deliver/pages/deliver_fail/view.dart';
import 'package:honeypotflutter/modules/deliver/pages/deliver_success/view.dart';
import 'package:honeypotflutter/public/public_alert.dart';
import 'package:honeypotflutter/public/public_total.dart';
import 'package:honeypotflutter/publicBussine/express/bussine_express_logic.dart';
import 'package:honeypotflutter/publicBussine/express/bussine_express_model.dart';
import 'package:scan/scan.dart';
import '../../model/deliver_orderInfo_model.dart';
import '../../model/deliver_param.dart';
import '../../model/deliver_result.dart';

class DeliverOrderLogic extends GetxController {
  /// 订单号
  TextEditingController orderIDController = TextEditingController();

  /// 手机号
  TextEditingController phoneController = TextEditingController();

  /// 扫码
  ScanController controller = ScanController();

  /// 开启
  RxBool open = RxBool(true);

  /// 展示快递公司
  RxBool showExpress = RxBool(false);

  /// 柜机信息
  Map<String, dynamic> arg = {};

  /// 订单信息
  DeliverOrderInfoModel orderInfoModel = DeliverOrderInfoModel();

  /// 快递信息
  ExpressLogic _expressLogic = Get.find<ExpressLogic>();

  DeliverParam _param = DeliverParam();

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    arg = Get.arguments ?? {};
    _param.deviceId = arg["deviceId"];
    if (arg["type"] == DeliverBoxType.max) {
      _param.doorType = 1;
    }
    if (arg["type"] == DeliverBoxType.middle) {
      _param.doorType = 2;
    }
    if (arg["type"] == DeliverBoxType.small) {
      _param.doorType = 3;
    }
  }

  /*
  * 快递公司
  * */
  selectExpress() async {
    List<String> expressList = _expressLogic.expressUserList.map((e) => e.fullName ?? "").toList();
    expressList.add("添加快递公司");
    List<int> expressIDList = _expressLogic.expressUserList.map((e) => e.courierCompanyCode ?? -1).toList();
    int selectID = -1;
    if (orderInfoModel.courierCompanyCode != null) {
      selectID = expressIDList.indexOf(orderInfoModel.courierCompanyCode!);
    }
    HLAlert.showSheet(
      title: "请选择快递公司",
      menuList: expressList,
      initialSelectedActionKey: selectID,
      action: (index, value) {
        if (index == expressList.length - 1) {
          Get.to(DeliverExpressPage());
          return;
        }
        MGExpressModel model = _expressLogic.expressUserList[index];
        orderInfoModel.courierCompanyCode = model.courierCompanyCode;
        orderInfoModel.courierCompanyName = model.fullName;
        update();
      },
    );
  }

  /*
  * 请求订单信息
  * */
  requestOrderInfo(String expressNo) async {
    orderIDController.text = expressNo;
    try {
      orderInfoModel = await DeliverApi.orderInfo(
        expressNo: expressNo,
        disPlayNo: arg["disPlayNo"],
        deviceId: arg["deviceId"],
      );
      String phone = "";
      if (orderInfoModel.phoneNumbers != null) {
        String firstPhone = orderInfoModel.phoneNumbers!.first;
        if (firstPhone != "FAIL") {
          phone = firstPhone;
        }
      }
      phoneController.text = phone;
      update();
    } catch (error) {}
  }

  /*
  * 重置
  * */
  reset() async {
    orderInfoModel = DeliverOrderInfoModel();
    update();
    controller.resume();
  }

  /*
  * 投递
  * */
  deliver() async {
    if (orderIDController.text.isEmpty) {
      HLTotal.total("请输入快递单号");
      return;
    }

    if (phoneController.text.isEmpty) {
      HLTotal.total("请输入手机号码");
      return;
    }

    if (open.value && orderInfoModel.courierCompanyName == null && orderInfoModel.courierCompanyName!.isEmpty) {
      HLTotal.total("请选择快递公司");
      return;
    }

    if (orderInfoModel.expressNoType == 0 && !GetUtils.isPhoneNumber(phoneController.text)) {
      HLTotal.total("请输入正确手机号码");
      return;
    }
    _param.expressNoType = orderInfoModel.expressNoType;
    _param.expressNo = orderIDController.text;
    _param.orderPhone = phoneController.text;
    _param.companyCode = orderInfoModel.courierCompanyCode;
    DeliverResult result = await DeliverApi.deliver(_param);
    if (result.isSucceed != null && result.isSucceed!) {
      Get.to(DeliverSuccessPage(), arguments: result);
    } else {
      Get.to(DeliverFailPage(), arguments: result);
    }
  }
}
